//
//  DoorMessage.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/27.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "DoorMessage.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "TransRule2ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <AMapSearchKit/AMapSearchKit.h>

@interface DoorMessage ()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource,AMapSearchDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;



@property(nonatomic,strong) UITextField *textName;
@property(nonatomic,strong) UITextField *textPhone;
@property(nonatomic,strong) UITextField *textAddress;
@property(nonatomic,strong) UITextField *textMarket;
@property(nonatomic,strong) UITextField *textAddressD;
@property(nonatomic,strong) UITextField *textDistance;
@property(nonatomic,strong) UITextField *textStartPrice;
@property(nonatomic,strong) UITextField *textTransPrice;


//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;



//记录设置textfield的字符串
//@property(nonatomic,strong) NSString *textName1;
//@property(nonatomic,strong) NSString *textPhone1;
//@property(nonatomic,strong) NSString *textAddress1;
@property(nonatomic,strong) NSString *textMarket1;
@property(nonatomic,strong) NSString *textAddressD1;
@property(nonatomic,strong) NSString *textDistance1;
@property(nonatomic,strong) NSString *textStartPrice1;
@property(nonatomic,strong) NSString *textTransPrice1;


//产生pickView
@property(nonatomic,strong) UIPickerView *shichangPickview;



//区域批发商地址pickView
@property(nonatomic,strong) UIPickerView *pickView2;
@property(nonatomic,strong) UIView *pick2View;





//添加数字键盘模板
@property(nonatomic,strong) UIView *numberView;


//获取批发商信息list
@property(nonatomic,strong) NSMutableArray *list;

@property(nonatomic) NSInteger currentSelectInCom0;



//block从下一个页面传递回来的数据
//创建一个数组接收从下一个页面返回来的数值
@property(nonatomic,strong) NSArray *backArray;

//地图正向地理编码
@property(nonatomic,strong) AMapSearchAPI *search;
@property(nonatomic,strong) AMapGeocodeSearchRequest *geo;

@end

@implementation DoorMessage


//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}



-(void)viewWillAppear:(BOOL)animated
{
    
    
    
//    NSLog(@"wwwwww  %@ %@   %@",self.backArray,self.dataHandler.latitude,self.dataHandler.longtitude);
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.backBtn.hidden = NO;
    self.titleLabel.text = @"门店信息";
    self.rightBut.hidden = NO;
//    [self initBottom];
    [self.rightBut setTitle:@"保存" forState:UIControlStateNormal];
    self.rightBut.titleLabel.font = [UIFont systemFontOfSize:17];
    
    
    self.list = [NSMutableArray arrayWithCapacity:0];
    self.currentSelectInCom0 = 0;

    //配置用户Key
    [AMapSearchServices sharedServices].apiKey = gaodeSecrect;
    //初始化对象
    self.search = [[AMapSearchAPI alloc]init];
    self.search.delegate = self;
    //构造AMapGeocodeSearchRequest对象，address为必选项，city为可选项
     self.geo = [[AMapGeocodeSearchRequest alloc] init];

    

    
    //直接从本地获取修改后的数据
    self.textMarket1 = self.dataHandler.shichang;
    self.textAddressD1 = self.dataHandler.dizhi;
    self.textDistance1 =  self.dataHandler.fanwei;
    self.textStartPrice1 = self.dataHandler.jiage;

    




    
    
    
    
    
    
    if (ViewHeight == 480.0) {
        
        
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0) ;
        
        
    }

    
    
    
}


//
//
//-(void)initBottom
//{
//    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0,ViewHeight -44, ViewWidth, 44)];
//    btn.backgroundColor = BenSe_COLOR;
//    [btn setTitle:@"保存" forState:UIControlStateNormal];
//    [self.view addSubview:btn];
//    [btn addTarget:self action:@selector(rightButClick:) forControlEvents:UIControlEventTouchUpInside];
//}


#pragma mark 进行保存门店信息操作
-(void)rightButClick:(id)sender
{
    
    
    

    
    
    if (self.textMarket.text.length == 0 || self.textAddressD.text.length == 0 || self.textDistance.text.length == 0 || self.textStartPrice.text.length == 0  ) {
        
        
        [self.view makeToast:@"请完善信息"];
        return;
    }
    
    
    
#warning  海拔经纬度还没改
    
    //修改经纬度
    NSString *address = [NSString stringWithFormat:@"%@%@",[[self.dataHandler.userDic objectForKey:@"wholesaler"] objectForKey:@"city"],self.textAddressD.text];
    self.geo.address = address;
    //发起正向地理编码
    [self.search AMapGeocodeSearch:self.geo];
    
    //先获取经纬度再上传到服务器上

  
}


//实现正向地理编码的回调函数
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
    
  
    if(response.geocodes.count == 0)
    {
        
        [self.view makeToast:@"解析不到地址,重新输入"];
        return;
    }
    //通过AMapGeocodeSearchResponse对象处理搜索结果
    NSString *strCount = [NSString stringWithFormat:@"count: %d", response.count];
    NSString *strGeocodes = @"";
    for (AMapTip *p in response.geocodes) {
        strGeocodes = [NSString stringWithFormat:@"%@\ngeocode: %@", strGeocodes, p.description];
//        NSLog(@"hhhiqhq %f  %f",p.location.latitude , p.location.longitude);
        
        
        //获得经纬度进行保存到本地
        self.dataHandler.latitude =  [NSString stringWithFormat:@"%f",p.location.latitude];
        self.dataHandler.longtitude =[NSString stringWithFormat:@"%f", p.location.longitude];
        
        
        
        
    }
    
    
    
    //判断
    
    NSString *str1 =    self.dataHandler.juli;
    NSString *str2  =   self.dataHandler.yunfei;
    NSString *str3 =    self.dataHandler.dianshu;
    NSString *str4 =    self.dataHandler.zjyunfei;
    
    
    
    if (self.backArray.count == 0) {
        
        
    }
    else
    {
        str1 = self.backArray[0];
        str2 = self.backArray[1];
        str3 = self.backArray[2];
        str4 = self.backArray[3];
        
        
    }

    
    
    NSString *mm = md5(self.dataHandler.password);

    
    
    
    NSString *lati = [NSString stringWithFormat:@"%@",self.dataHandler.latitude];
    
    NSString *logi = [NSString stringWithFormat:@"%@",self.dataHandler.longtitude];
    
    
    NSLog(@"yyyy2  %@   %@   %@  %@  %@  %@ %@  %@  %@  %@  %@ %@ %@ %@",self.dataHandler.phone,self.dataHandler.uToken,self.textName.text, mm ,self.textAddressD.text, lati,logi,self.textMarket.text,self.textDistance.text,self.textStartPrice.text ,str1,str3,str2,str4);
    
    
    NSString *urlStr = [NSString stringWithFormat:@"%@update_wholesaler",SERVER_ADDRESS2];
    NSDictionary *parmas = @{@"wTel" : self.dataHandler.phone ,@"uToken" : self.dataHandler.uToken, @"wholesaler_name" : self.textName.text, @"passwd" : mm ,@"address" : self.textAddressD.text, @"addX" : lati ,@"addY" : logi ,@"market" : self.textMarket.text , @"push_radius" : self.textDistance.text,@"start_price" : self.textStartPrice.text, @"near_range" : str1 ,@"far_range" : @"" ,@"start_point" : str3 , @"near_price" : str2 , @"over_price" : str4};
    
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    
    [self.manager POST:urlStr parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0) {

            //重新进行数据本地保存，经纬度重新保存
            [self.dataHandler saveLocalData:@"addX" value:self.dataHandler.latitude];
            [self.dataHandler saveLocalData:@"addY" value:self.dataHandler.longtitude];
          
            //具体地址
            self.dataHandler.dizhi = self.textAddressD.text;
            [self.dataHandler saveLocalData:@"address" value:self.dataHandler.dizhi];
            
            
            
            
            
            
            //送货范围
            self.dataHandler.fanwei = self.textDistance.text;
            
            [self.dataHandler saveLocalData:@"push_radius" value:self.dataHandler.fanwei];
            
            //起送价格
            self.dataHandler.jiage = self.textStartPrice.text;
            [self.dataHandler saveLocalData:@"start_price" value:self.dataHandler.jiage];
            
            
            
            //保存的市场（单个）
            
            self.dataHandler.shichang = self.textMarket.text;
            [self.dataHandler saveLocalData:@"market" value:self.dataHandler.shichang];
            
            
            
            //判断如果有backarray有数据，进行重新保存，如果没有不进行保存
            if (self.backArray.count == 0) {
                
                
                //不进行操作
            }
            else
            {
                
                
                //送货距离
                self.dataHandler.juli = str1;
                [self.dataHandler saveLocalData:@"near_range" value:self.dataHandler.juli];
                
                
                
                //送货运费
                self.dataHandler.yunfei = str2;
                [self.dataHandler saveLocalData:@"near_price" value:self.dataHandler.yunfei];
                
                
                
                //起送店数
                self.dataHandler.dianshu = str3;
                [self.dataHandler saveLocalData:@"start_point" value:self.dataHandler.dianshu];
                
                //额外增加运费
                self.dataHandler.zjyunfei = str4;
                [self.dataHandler saveLocalData:@"over_price" value:self.dataHandler.zjyunfei];
              
            }
            
            
            
            //            //进行地址正向解析获取数据
            //            NSString *address = [NSString stringWithFormat:@"%@%@",self.dataHandler.shi,self.textAddressD];
            //
            //            CLGeocoder *myGeocoder =[[CLGeocoder alloc]init];
            //            [myGeocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            //
            //                if ([placemarks count] > 0 && error == nil) {
            //
            //                    NSLog(@"Found %lu placemark(s)",(unsigned long)[placemarks count]);
            //                    CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
            //                    NSLog(@"longitude = %f",firstPlacemark.location.coordinate.longitude);
            //                    NSLog(@"Latitude = %f",firstPlacemark.location.coordinate.latitude);
            //
            //
            //                    //海拔经纬度保存
            //                    self.dataHandler.latitude = [NSString stringWithFormat:@"%f",firstPlacemark.location.coordinate.latitude];
            //                    self.dataHandler.longtitude = [NSString stringWithFormat:@"%f",firstPlacemark.location.coordinate.longitude];
            //
            //
            //
            //
            //                }
            //                else if ([placemarks count] == 0 && error == nil)
            //                {
            //
            //                    [self.view makeToast:@"解析不到地址,重新解析"];
            //                    return ;
            //
            //                }
            //                else if (error != nil)
            //                {
            //                    NSLog(@"An error occurred = %@",error);
            //                    NSString *error1 = [NSString stringWithFormat:@"%@",error];
            //                    [self.view makeToast:error1];
            //
            //                    return;
            //                }
            //                else
            //                {
            //                    [self.view makeToast:@"解析不到地址,重新解析"];
            //                    return ;
            //
            //
            //                }
            //
            //
            //            }];
            
            
            
            
            
            
            
            
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        else
        {
            
            
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result_msg"]];
            UIAlertView *alter =[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
            
            
            
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接网络失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
        
    }];
    
    
    

    
//    NSString *result = [NSString stringWithFormat:@"%@ \n %@", strCount, strGeocodes];
    
//    31.033722   121.145927    <31.231707, 121.472641>   <31.290812, 121.482231>
}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 8;


}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    return 50;

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 0.1;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell10";
    
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell ==nil) {
        
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:17];
        
    }
    switch (indexPath.section) {
        case 0:
            
            
            switch (indexPath.row) {
                case 0:
                    
                    if (self.textName == nil) {
                        
                    
                    
                    cell.textLabel.text = @"批发商名称";
                    self.textName =[[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.7, 50)];
                    self.textName.font = [UIFont systemFontOfSize:16];
                    self.textName.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textName];
                    self.textName.enabled = NO;
                    
                    self.textName.delegate = self;
                    
                    self.textName.text = self.dataHandler.name;
                    
                    self.textName.textColor = [UIColor lightGrayColor];
                    }
                    
                    break;
                case 1:
                    
                    
                    if (self.textPhone == nil) {
                        
                    
                    cell.textLabel.text = @"批发商电话";
                    self.textPhone = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth*0.7, 50)];
                    self.textPhone.font = [UIFont systemFontOfSize:16];
                    self.textPhone.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textPhone];
                    self.textPhone.enabled = NO;
                    
                    self.textPhone.delegate = self;
                    
                    
                    self.textPhone.text = self.dataHandler.phone;
                    self.textPhone.textColor = [UIColor lightGrayColor];
                        
                    }
                    break;
                    
                    
                case 2:
                    
                    if (self.textAddress == nil) {
                        
                    
                    cell.textLabel.text = @"批发商地区";
                    self.textAddress =  [[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.7, 50)];
                    self.textAddress.font = [UIFont systemFontOfSize:16];
                    self.textAddress.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textAddress];
                    self.textAddress.enabled = NO;
                    
                    self.textAddress.delegate = self;
                    self.textAddress.text = self.dataHandler.newdizhi;
                       self.textAddress.textColor = [UIColor lightGrayColor];
                    
                    }
                    break;
                    
                    
                case 3:
                    if (self.textMarket == nil) {
                        
                    
                    cell.textLabel.text = @"所 属 市 场";
                    self.textMarket = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.7, 50)];
                    self.textMarket.font = [UIFont systemFontOfSize:14];
                    self.textMarket.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textMarket];
                    self.textMarket.placeholder = @"请填写所属市场";
                    self.textMarket.textColor = BenSe_COLOR;
                    
                    self.textMarket.delegate = self;
                    
//                    self.textMarket.text = [[self.dataHandler.userDic objectForKey:@"wholesaler"] objectForKey:@"market"];
                    
                    
                    self.textMarket.text = self.textMarket1;
                    
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//                    self.textMarket.textColor = [UIColor lightGrayColor];
                    self.textMarket.enabled = NO;
                    }
                    
                    break;
                    
                    
                 case 4:
                    
                    
                    if (self.textAddressD == nil) {
                        
                    
                    cell.textLabel.text = @"具 体 地 址";
                    
                    
      
                    
                    
                    self.textAddressD = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.7, 50)];
                    self.textAddressD.font = [UIFont systemFontOfSize:16];
                    self.textAddressD.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textAddressD];
                        
//                    self.textAddressD.text = [NSString stringWithFormat:@"%@",[[self.dataHandler.userDic objectForKey:@"wholesaler"] objectForKey:@"address"]];
                    
                    
                    self.textAddressD.text = self.textAddressD1;
                    self.textAddressD.textColor = BenSe_COLOR;
                    self.textAddressD.delegate = self;
//                       self.textAddressD.textColor = [UIColor lightGrayColor];
                    }
                    break;
                  
                    
                 case 5:
                    
                    
                    if (self.textDistance == nil) {
                        
                    
                    cell.textLabel.text = @"送 货 范 围";
                    
                    
                    
                    
                    
                    self.textDistance = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.7, 50)];
                    self.textDistance.font = [UIFont systemFontOfSize:16];
                    self.textDistance.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textDistance];
                    self.textDistance.textColor = BenSe_COLOR;
//                    self.textDistance.text =[NSString stringWithFormat:@"%@",[[self.dataHandler.userDic objectForKey:@"wholesaler"] objectForKey:@"push_radius"]];
                    
                    
                    self.textDistance.text = [NSString stringWithFormat:@"%@",self.textDistance1];
                    

                    self.textDistance.delegate = self;
                    
//                    self.textDistance.textColor = [UIColor lightGrayColor];
                    
                    
                    self.textDistance.keyboardType = UIKeyboardTypeDefault;
                    }
                    
                    break;
                    
                    
                    
                 case 6:
                    if (self.textStartPrice == nil) {
                    cell.textLabel.text = @"起 送 价 格";
                    self.textStartPrice = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.7, 50)];
                    self.textStartPrice.font = [UIFont systemFontOfSize:16];
                    self.textStartPrice.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textStartPrice];
                    self.textStartPrice.placeholder = @"请填写起送价格";
                    
                    self.textStartPrice.textColor = BenSe_COLOR;

                    
                    self.textStartPrice.delegate = self;
                    
                    self.textStartPrice.text = [NSString stringWithFormat:@"%@",self.textStartPrice1];
//                    self.textStartPrice.textColor = [UIColor lightGrayColor];
                    
                    self.textStartPrice.keyboardType = UIKeyboardTypePhonePad;
                    
                        
                    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth*0.7, 0, 30, 50)];
                    lbl.text = @"元";
                    lbl.textColor = [UIColor lightGrayColor];
                    [cell addSubview:lbl];
                        
                        
                    }
                    
                    break;
                    
                 case 7:
                    
                    if (self.textTransPrice == nil) {
                        
                    
                    
                    cell.textLabel.text = @"运 费 规 则";
                    self.textTransPrice = [[UITextField alloc]initWithFrame:CGRectMake(110, 0, ViewWidth *0.7, 50)];
                    self.textTransPrice.font = [UIFont systemFontOfSize:16];
                    self.textTransPrice.textAlignment = NSTextAlignmentLeft;
                    [cell addSubview:self.textTransPrice];
                    self.textTransPrice.placeholder = @"已设置";
                    
                    self.textTransPrice.delegate = self;
                    
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    
                     self.textTransPrice.textColor = [UIColor lightGrayColor];
                        
                    self.textTransPrice.enabled = NO;
                    
                    
                    }

                    break;
                    
              
                default:
                    break;
            }
            
            
            break;
            
        default:
            break;
    }

 
    
    
    return cell;
    
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    if (indexPath.row == 3) {
        
        
        
        //获得城市信息。获得批发商的商场
        
        
        
        [self sendAPI];

        
        
        
        
        
    }
    else if (indexPath.row == 7)
    {
    
        TransRule2ViewController *rule2 = [[TransRule2ViewController alloc]initWithNibName:nil bundle:nil];
        
        
        
        
        rule2.block = ^(NSArray *message)
        {
            self.backArray = message;
            
            self.dataHandler.yunfeiArray = [self.backArray copy];
            
            
            
            
            
        };

        
        [self.navigationController pushViewController:rule2 animated:YES];
    
    
    
    
    }
    
    
    



}


#pragma mark 获取批发商市场


-(void) sendAPI
{

    
    //获取市场批发商
    NSString *urlStr = [NSString stringWithFormat:@"%@get_market",SERVER_ADDRESS];
    NSString *city = [NSString stringWithFormat:@"%@",self.dataHandler.shi];
    
    NSDictionary *parmas = @{@"parent_id" : city};
    
     [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.manager POST:urlStr parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        
         if ([responseObject[@"error_flag"] intValue] == 0) {
             
             
             
                        NSArray *array = responseObject [@"data"];
             
             if (array.count == 0) {
                 
                 [self.view makeToast:@"暂未获得该地区的市场信息"];
                 
             }
             else
             {
                 
                 self.list = [NSMutableArray arrayWithArray:array];
                 
                 
                 //获得数据进行赋操作
                 [self setEditing:NO];
                 
                 if (self.pick2View == nil) {
                     
                     self.pick2View = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ViewWidth, ViewHeight-64)];
                     
                     
                     self.pick2View.backgroundColor = [UIColor clearColor];
                     [self.view addSubview:self.pick2View];
                     
                     
                     
                 }
                 if (self.pickView2 == nil) {
                     
                     self.pickView2 = [[UIPickerView alloc]initWithFrame:CGRectMake(0, ViewHeight *0.65, ViewWidth, ViewHeight *0.35)];
                     self.pickView2.backgroundColor = [UIColor whiteColor];
                     self.pickView2.delegate = self;
                     self.pickView2.dataSource = self;
                     
                     [self.pick2View addSubview:self.pickView2];
                     
                     
                     
                     
                 }
                 //进行刷新所有的数据
                 [self.pickView2 reloadAllComponents];
                 self.pick2View.hidden = NO;
                 self.pickView2.hidden = NO;
                 
                 //给蒙版添加手势进行去除手势
                 //点击空白收回键盘
                 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Tapped4:)];
                 [self.pick2View addGestureRecognizer:tap];
                 
                 
                 
             }

             
             
             
             
             
             [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
             
             NSLog(@"IIIIIIIII  %@",self.list);
             [self.tableView reloadData];
         }
        
        
       
        else
        {
        
            
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result_msg"]];
            UIAlertView *alter =[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
        
        
        
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接网络失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
        
    }];



}



#pragma mark 手势进行操作去除蒙版
-(void)Tapped4:(id)sender
{
    
    
    
    
    self.pickView2.hidden = YES;
    self.pick2View.hidden = YES;
    
    
    
}




#pragma mark 进行pickView的设置

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    
    
//    NSLog(@"uiuiuiu  %@",self.list);
    
    
    if (pickerView == self.pickView2) {
        
        return 1;
        
        
        
    }
    else
    {
        
        return 1;
        
    }
    
    
    
}



//指定pickView中每一列的个数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    
    if (pickerView == self.pickView2) {
        
        return self.list.count;
    }
    else
    {
        
        return 1;
    }
    
}



-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 30;
    
}



#pragma mark pickViewDelegate协议方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
    
    if (pickerView == self.pickView2) {
        
        
        NSString *str = [NSString stringWithFormat:@"%@",[self.list objectAtIndex:row]];
        return str;
        
    }
    else
    {
        
        
        
        return @"";
    }
    
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (pickerView == self.pickView2) {
        
                
        self.textMarket1 = [self.list objectAtIndex:row];
        self.textMarket.text = self.textMarket1;
        
    }
    
}







#pragma mark 进行textfield的end赋值编辑操作
-(void)textFieldDidEndEditing:(UITextField *)textField
{

    if (textField == self.textMarket) {
        
        self.textMarket1 = self.textMarket.text;
        
        
    }

    else if (textField == self.textAddressD)
    {
    
        self.textAddressD1 = self.textAddressD.text;
    
    }
    else if (textField == self.textDistance)
    {
    
        self.textDistance1 = self.textDistance.text;
    
    }
    else if (textField == self.textStartPrice)
    {
    
        self.textStartPrice1 = self.textStartPrice.text;
    
    
    }
    





}





//开始编辑的时候收回数字键盘操作
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    
    
    
    if (textField == self.textAddressD || textField == self.textDistance || textField == self.textStartPrice  )
    {
        
        
        if (self.numberView == nil) {
            
            self.numberView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight-64)];
            self.numberView.alpha = 0.2;
            self.numberView.backgroundColor = [UIColor whiteColor];
            [self.tableView addSubview:self.numberView];
            
            
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Tapped2:)];
        
        [self.numberView addGestureRecognizer:tap];
        
        
        
        
        
        
        
        
        return YES;
        
    }
    
    
    return YES;
    
    
}




#pragma mark 如果没有没有doneButton的产生就收回就点击空白收回键盘，收回键盘操作
-(void)Tapped2:(UITapGestureRecognizer*)gestureRecognizer
{
    
    
    
    //伸缩表格
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -140) {
            self.view.y = 0;
            
            
        }
    }completion:^(BOOL finished){
        
        
    }];
    
    
    [self.numberView removeFromSuperview];
    self.numberView = nil;
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    
    
    
}




#pragma mark 上移键盘操作
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    if (textField == self.textAddressD || textField == self.textDistance  || textField == self.textStartPrice)  {
        //上移键盘操作
        
        
        //伸缩表格
        [UIView animateWithDuration:0.25 animations:^{
            if (self.view.y == 0) {
                self.view.y = -140;
            }
        }completion:^(BOOL finished){
            
            
            
            
            
        }];
        
        
        
        
    }
    
    
    
    
}



-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    //伸缩表格
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -140) {
            self.view.y = 0;
            
            
        }
    }completion:^(BOOL finished){
        
        
    }];
    
    
    return YES;
    
}




@end
